package com.woniuxy.user.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.woniuxy.commons.entity.Escort;
import com.woniuxy.commons.entity.User;
import com.woniuxy.commons.service.FeignEscortService;
import com.woniuxy.commons.util.MD5Util;
import com.woniuxy.commons.util.ResponseResult;
import com.woniuxy.user.mapper.UserMapper;
import com.woniuxy.user.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
@Service
@Slf4j
public class UserServiceImpl implements UserService {

    @Resource
    private UserMapper userMapper;
    @Resource
    private FeignEscortService feignEscortService;




    @Override
    @Transactional
    public boolean register(String userInfo) {
        JSONArray array = JSONObject.parseArray(userInfo);
        log.info(array.toString());
        //user对象
        User user = JSON.parseObject(array.get(0).toString(), User.class);
        log.info(user.toString());

        if(user.getType()!=0 && user.getType()!=1 && user.getType()!=2){
            //如果user没加type就注册不了
            return false;
        }
        try {
            user.setPassword(MD5Util.md5(user.getPassword()));
        } catch (Exception e) {
            e.printStackTrace();
        }
        Boolean res= userMapper.register(user);
        if (!res){
            return false;
        }
        System.out.println(user.getId());

        //判断传入的user是哪种类型
        switch (user.getType()){
            //残疾人
            case 0:
                return true;
            //监护人
            case 1:
                return true;
            //陪护员
            case 2:
                //陪护员对象
                Escort escort = JSON.parseObject(array.get(1).toString(), Escort.class);
                escort.setUserId(user.getId());
                System.out.println(escort);
                ResponseResult responseResult = feignEscortService.addEscort(escort);
                return responseResult.getCode() == 200;

        }
        return false;


    }

    /**
     *
     * @param user
     * @return
     * 0表示账号和密码错误
     * 1为登录成功
     * 1为账号未激活
     */
    @Override
    public int login(User user) {
        try {
            user.setPassword(MD5Util.md5(user.getPassword()));
            log.info(user.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
        User returnUser = userMapper.login(user);
        log.info(returnUser.toString());
        if (returnUser == null) {
            return 0;
        }

        //判断该用户是否激活
        Integer status = userMapper.isActiveByUsername(user.getUsername());
        log.info(status.toString());
        //未激活
        if (status != 1){
            return 2;
        }
        return 1;
    }
//根据条件查询用户
    @Override
    public List<User> isExist(User user) {
        return userMapper.isExist(user);
    }

    @Override
    public List<String> findPermsByUsername(String username) {

        return userMapper.findPermsByUsername(username);
    }

    @Override
    public boolean isExistUsername(String username) {
        User user = userMapper.isExistUsername(username);

        return user == null;
    }

    @Override
    public boolean isExistPhone(String phone) {
        User user = userMapper.isExistPhone(phone);
        return user == null;
    }

    @Override
    @Transactional
    public boolean changePassword(User user) {

        return userMapper.changePassword(user);
    }

    @Override
    public boolean isPasswordSame(User user) {
        try {
            user.setPassword(MD5Util.md5(user.getPassword()));
        } catch (Exception e) {
            e.printStackTrace();
        }

        User returnUser = userMapper.isPasswordSame(user);

        return returnUser == null;
    }
}
